<?php
/**
 * Created by PhpStorm.
 * User: dt
 * Date: 2017/3/6
 * Time: 13:36
 */
namespace app\utils;

use Yii;

class MockUtil {

    //单例
    private static $instance = null;
    //数据缓存时间
    private static $cacheExpire = 36000;
    //最大数据量
    private static $maxCount = 100;

    private static $modelType = [
        'house',    //楼盘
        'building', //栋
        'unit',     //单元
        'school',   //学校
        'subway',   //地体
        'developer',//开发商
        'city',//城市管理
        'dictionary',//字典
    ];

    private function __construct()
    {
    }

    public static function getInstance($options = [])
    {
        if (empty(self::$instance)) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }

    /**
     * 类型检测
     * @param $type
     */
    private function _keyValidate($type)
    {
        if (!in_array($type,self::$modelType)) {
            exit('类型不合法');
        }
    }

    /**
     * 插入数据
     * @param $token //登录用户标识
     * @param $type //模块类型，唯一
     * @param $data //数据
     * @param $primaryKey  //数据主键字段
     * @return int|mixed
     */
    public function insert($token, $type, $data, $primaryKey)
    {
        self::_keyValidate($type);
        $cache = Yii::$app->cache;
        if ($cache->exists($token.$type.'count')) {
            $cacheCount = $cache->get($token.$type.'count');
            $cache->set($token.$type.'count',($cacheCount+1),self::$cacheExpire);
            $count = $cacheCount+1;
        } else {
            $cache->add($token.$type.'count',1,self::$cacheExpire);
            $count = 1;
        }
        $data[$primaryKey] = $count;
        $cache->add($token.$type.$count,$data,self::$cacheExpire);
        return $count;
    }

    /**
     * 获取列表
     * @param $token
     * @param $type
     * @return array
     */
    public function getList($token, $type)
    {
        self::_keyValidate($type);
        $cache = Yii::$app->cache;
        $resultData = [];
        for ($i = 1; $i<self::$maxCount; $i++) {
            if ($cache->exists($token.$type.$i)) {
                $resultData[] = $cache->get($token.$type.$i);
            }
        }
        return $resultData;
    }

    /**
     * 获取详情
     * @param $token
     * @param $type
     * @param $id
     * @return mixed|string
     */
    public function getDetails($token, $type, $id)
    {
        self::_keyValidate($type);
        $cache = Yii::$app->cache;
        for ($i = 1; $i<self::$maxCount; $i++) {
            if ($cache->exists($token.$type.$i) && $i == $id) {
                $resultData = $cache->get($token.$type.$i);
                return $resultData;
            }
        }
        return [];
    }

    public function edit($token, $type, $data, $primaryKey)
    {
        self::_keyValidate($type);
        $id = $data[$primaryKey];
        $cache = Yii::$app->cache;
        if ($cache->exists($token.$type.$id)) {
            $data[$primaryKey] = $id;
            $cache->set($token.$type.$id,$data,self::$cacheExpire);
        }
        return $id;
    }

    /**
     * 删除数据
     * @param $token
     * @param $type
     * @param $id
     */
    public function delete($token, $type, $id)
    {
        self::_keyValidate($type);
        $cache = Yii::$app->cache;
        $ids = explode(',', $id);
        for ($i = 1; $i<self::$maxCount; $i++) {
            if ($cache->exists($token.$type.$i) && in_array($i,$ids)) {
                $cache->delete($token.$type.$i);
            }
        }
    }

    /**
     * 清数据
     * @param $token
     * @param $type
     */
    public function refresh($token, $type)
    {
        self::_keyValidate($type);
        $cache = Yii::$app->cache;
        for ($i = 1; $i<self::$maxCount; $i++) {
            if ($cache->exists($token.$type.$i)) {
                $cache->delete($token.$type.$i);
                $cache->delete($token.$type.'count');
            }
        }
    }
}